package com.inspur.gs.sbssdl.sbwhgl.sbrh.sbrh.front.sbsslutree.voactions;
import com.inspur.edp.bff.api.manager.context.QueryContext;
import com.inspur.edp.bff.spi.action.query.AbstractQueryAction;
import com.inspur.edp.caf.db.dbaccess.DynamicResultRow;
import com.inspur.edp.cef.entity.dependenceTemp.Pagination;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.common.commonudt.pathhierarchyinfo.entity.IPathHierarchyInfo;
import com.inspur.edp.qdp.bql.api.IBqlExecuter;
import com.inspur.edp.qdp.bql.api.OptionType;
import io.iec.edp.caf.runtime.config.BqlBeanUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

public class queryDataVOAction extends AbstractQueryAction {

	public queryDataVOAction(QueryContext context){
		super(context);
	}

	private final String queryClass = "select DEVCLASS from SBSS_SBRH_STAND group by DEVCLASS";

	private final String queryStand = "select DEVCLASS,DEVSPEC from SBSS_SBRH_STAND  group by DEVCLASS,DEVSPEC";

	private final IBqlExecuter bqlExecuter = BqlBeanUtil.getAppCtx().getBean(IBqlExecuter.class);

	private int classSeq = 1;

	public int getClassSeqAndAdd(){
		return classSeq++;
	}

	/**
	 * 构建  Tree信息 Map<String,Map<String,Int>>
	 *     以当前举例，保养需要 设备大类 -> 设备小类
	 * {
	 *   '设备大类1': {'seq':1,'bh':1}
	 *   '设备大类2': {'seq':1,'bh':2}
	 * }
	 *
	 * 设备名称中存储设备的编号 和 当前拥有的编号数量
	 *
	 * 将所有的父级全部设定好
	 * {
	 * 	   id: uuid,
	 *     name: '设备大类1',
	 *     TreeInfo:{
	 *         Path: 0001,
	 *         isDetail: false,
	 *         Layer: 1,
	 *     }
	 * }
	 * 再编写子集
	 * {
	 * 	   id: uuid,
	 *     name: '设备小类1',
	 *     TreeInfo:{
	 *         Path: 00010001,
	 *         isDetail: false,
	 *         Layer: 1,
	 *     }
	 * }
	 */
	@Override
	public void execute() {

		bqlExecuter.getOptions().setOptionType(OptionType.ExecuteSql);

		List<DynamicResultRow> classRow = bqlExecuter.executeSelectStatement(queryClass, new ArrayList<>());
		HashMap<String, HashMap<String, Integer>> parentTreeInfo = new HashMap<>();

		// 构建基础大类info
		ArrayList<IEntityData> result = new ArrayList<>();
		for (DynamicResultRow drr : classRow) {
			String name = drr.get("DEVCLASS").toString();
			HashMap<String, Integer> drrMap = new HashMap<>();
			drrMap.put("bh", getClassSeqAndAdd());
			drrMap.put("seq", 1);
			parentTreeInfo.put(name, drrMap);

			IEntityData vo = getContext().createData();
			vo.setID(UUID.randomUUID().toString());
			vo.setValue("name", name);
			vo.setValue("className", "className");

			IPathHierarchyInfo treeInfo = (IPathHierarchyInfo) vo.getValue("TreeInfo");
			treeInfo.setIsDetail(false);
			treeInfo.setLayer(1);
			treeInfo.setPath(String.format("%04d", drrMap.get("bh")));

			result.add(vo);
		}

		List<DynamicResultRow> specRow = bqlExecuter.executeSelectStatement(queryStand, new ArrayList<>());

		// 构建小类info
		for (DynamicResultRow drr : specRow) {
			String className = drr.get("DEVCLASS").toString();
			String specName = drr.get("DEVSPEC").toString();

			HashMap<String, Integer> classInfo = parentTreeInfo.get(className);

			Integer bh = classInfo.get("bh");
			Integer seq = classInfo.compute("seq", (k, i) -> i + 1);

			IEntityData vo = getContext().createData();
			vo.setID(UUID.randomUUID().toString());
			vo.setValue("name", specName);
			vo.setValue("className", className);

			IPathHierarchyInfo treeInfo = (IPathHierarchyInfo) vo.getValue("TreeInfo");
			treeInfo.setIsDetail(true);
			treeInfo.setLayer(2);
			treeInfo.setPath(String.format("%04d%04d", bh, seq));

			result.add(vo);
		}
		Pagination pageInfo = getQueryContext().getFilter().getPagination();
		pageInfo.setPageCount(result.size());
		pageInfo.setTotalCount(result.size());

		HashMap<String, ArrayList<IEntityData>> queryresult = new HashMap<>();
		queryresult.put("queryresult", result);
		getQueryContext().setQueryResult(result);
		setResult(queryresult);
	}
}
